<?php

namespace Kunfeng\Dify\Exception;

/**
 * Dify API 异常类
 */
class DifyApiException extends \Exception
{
    private $statusCode;
    private $responseData;

    public function __construct(
        string $message,
        int $statusCode = 0,
        array $responseData = [],
        \Throwable $previous = null
    ) {
        parent::__construct($message, 0, $previous);
        $this->statusCode = $statusCode;
        $this->responseData = $responseData;
    }

    /**
     * 从 HTTP 响应创建异常
     */
    public static function fromResponse(int $statusCode, array $responseData): self
    {
        $message = $responseData['message'] ?? 'Unknown API error';
        return new self($message, $statusCode, $responseData);
    }

    /**
     * 获取错误详情
     */
    public function getErrorDetails(): array
    {
        return $this->responseData['error'] ?? [];
    }

    /**
     * 获取完整的响应数据
     */
    public function getResponseData(): array
    {
        return $this->responseData;
    }

    /**
     * 获取 HTTP 状态码
     */
    public function getStatusCode(): int
    {
        return $this->statusCode;
    }
}